<?php
class constellation{

	public function __construct(){
		require "/var/www/html/collection/libs/phpQuery.class.php";
	}
	public function getHD(){
		phpQuery::newDocumentFile('http://astro.sina.com.cn/fate/astro_aries.html?prourl=0'); 
		$content = pq(".date ul li")->html();
		$content =  iconv("gb2312", "utf-8",$content);
		$content = preg_replace("/<img src=\"(.+?)\".*?>/", "", $content);
		$hd = explode("<br>", $content);
		unset($hd[0]);
		for ($i=1; $i <= count($hd); $i++) { 
			$hd[$i] = trim($hd[$i]);
		}
		foreach ($hd as $k => $v) {
			$hd[$k] = explode(" ",$v);
		}
		preg_match ("/.*?(?=\()/", $hd[1][0],$match[0]);
		preg_match ("/\(.*?\)/", $hd[1][0],$match[1]);
		preg_match ("/(?<=\)).*/", $hd[1][0],$match[2]);
		foreach ($match as $k => $m) {
			$hd[1][$k] = $m[0];
		}
		return $hd;
	}

	public function getToday($constellation){
		$tabs = array();
		$tabs["today"] = self::getTabsByDate("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/day/".date("Ymd",time()));
		$tabs["tomorrow"] = self::getTabsByDate("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/day/".date("Ymd",time() + 24 * 60 *60));
		return $tabs;
	}

	private function getTabsByDate($urlWithDate){
		phpQuery::newDocumentFile($urlWithDate); 
		$tab = pq(".tab");
		$tabs = array();
		foreach ($tab as $k => $t) {
			$tabs[$k]["title"]  = pq($t)->find("h4")->text();
			if($k < 4){
				$tabs[$k]["tabcontent"] = count(pq($t)->find("p img"));
			}else{
				$tabs[$k]["tabcontent"] = pq($t)->find("p")->text();
			}
		}
		unset($tabs[9]);

		$content = pq(".lotconts")->text();
		$items["tabs"] = $tabs;
		$items["content"] = $content;
		unset($tabs[9]);
		return $items;
	}

	public function getWeekly($constellation){
		phpQuery::newDocumentFile("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/weekly/"); 
		$ps= pq(".right")->find("p");
		$hs = pq(".right")->find("h4");
		$content = array();
		$title = array();
		$star = array();
		foreach ($hs as $k => $h) {
			$title[$k ] = pq($h)->text();
			if($k == 1){
				$star[$k]["hasObject"] = count(pq($h)->parent("div")->find("em:first")->find("img"));
				$star[$k]["hasnotObject"] = count(pq($h)->parent("div")->find("em:last")->find("img"));
			}else{
				$star[$k ] = count(pq($h)->find("img"));
			}
		}
		foreach ($ps as $k => $p) {
			if($k == 0){
				$content[$k]= pq($p)->text();
			}elseif($k == 1){
				$content[$k]["hasObject"] = pq($p)->parent("div")->find("p:first")->text();
				$content[$k]["hasnotObject"] = pq($p)->parent("div")->find("p:last")->text();
			}elseif($k == 2){
				continue;
			}else{
				$content[$k-1]= pq($p)->text();
			}
		}
		$week = array();
		for($i = 0;$i <count($title);$i++){
			$week[$i]["title"] = $title[$i];
			$week[$i]["star"] = $star[$i];
		}
		$week["content"] = $content;
		return $week;
	}

	public function getMonthly($constellation){
		phpQuery::newDocumentFile("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/monthly/"); 
		$ps= pq(".right")->find("p");
		$hs = pq(".right")->find("h4");
		$content = array();
		$title = array();
		$star = array();
		foreach ($hs as $h) {
			$title[] = pq($h)->text();
			$star[] = count(pq($h)->find("img"));
		}
		foreach ($ps as $p) {
			$content[]= pq($p)->text();
		}
		$month = array();
		for($i = 0;$i <count($content);$i++){
			$month[$i]["content"] = $content[$i];
			$month[$i]["title"] = $title[$i];
			$month[$i]["star"] = $star[$i];
		}
		return $month;
	}

	public function getYear($constellation){
		phpQuery::newDocumentFile("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/year/"); 
		$ps= pq(".right")->find("p");
		$hs = pq(".right")->find("h4");
		$content = array();
		$title = array();
		$star = array();
		foreach ($hs as $h) {
			$title[] = pq($h)->text();
			$star[] = count(pq($h)->find("img"));
		}
		foreach ($ps as $p) {
			$content[]= pq($p)->text();
		}
		$year = array();
		for($i = 0;$i <count($content);$i++){
			$year[$i]["content"] = $content[$i];
			$year[$i]["title"] = $title[$i];
			$year[$i]["star"] = $star[$i];
		}
		return $year;
	}	

	public function getYear_love($constellation){
		phpQuery::newDocumentFile("http://vip.astro.sina.com.cn/iframe/astro/view/".$constellation."/year_love/"); 
		$love['girl'] = pq(".m_left")->html();
		$love['boy'] = pq(".m_right")->html();
		return $love;
	}
}

?>